<!-- Dom模板 -->
<template>
  <div class="box">
    <!-- Dom内容 -->

    <div class="tit-1">
      <span class="tit-1-left" @click="gohu"
        ><i class="iconfont icon-lt"></i
      ></span>
    </div>
    <div class="con animate__animated animate__bounceInUp">
      <div class="con-tit"></div>
      <div class="con-tit">{{ item.bookName }}——{{ item.name }}</div>
      <div class="con-center">{{ item.center }}</div>
      <div class="con-center"></div>
      <!-- 故事摘要 -->
      <div class="con-low">
        <p>故事摘要</p>
        <div class="zyCon" ref="zyCon">
          <p
            @click="clickP(item.time, index)"
            v-for="(item, index) in list"
            :key="index"
          >
            {{ item.name }}
          </p>
        </div>
      </div>
      <!-- 评论 -->
      <div class="comment-1" @click="goTalk">
        <div class="comment" v-if="talkSort === 0">
          <img :src="talkItem.userImg" alt="" />
          <span>{{ talkItem.userName }}</span>
          {{ talkItem.talkCon }}
        </div>
        <div class="comment" v-if="talkSort === 1">
          <img :src="talkItem.userImg" alt="" />
          <span>{{ talkItem.userName }}</span>
          <i class="iconfont icon-bofang"></i>
        </div>
        <div class="comment" v-if="talkSort === 2"></div>
        <div id="msg">
          <i class="iconfont icon-xiaoxi-weixuanzhong"></i>
        </div>
      </div>

      <!-- 音乐播放 -->
      <div class="music">
        <div class="plan">
          <span class="time" ref="nowTime">00:00</span>
          <span
            class="small"
            ref="small"
            @click="fastSing($event.offsetX)"
          ></span>
          <span class="dian" ref="dian" @touchmove="fastSings($event)"></span>
          <span class="long" ref="long" @click="fastSing($event.offsetX)">
            <audio
              :muted="true"
              class="audio"
              id="audio"
              ref="audio"
              @play="palyS"
              :src="singPath"
            >
              > 抱歉！ 浏览器版本太低，不支持音频
            </audio>
          </span>
          <span class="time" ref="molTime">60:00</span>
        </div>
        <div class="music-down">
          <span @click="tan" class="iconfont icon-yinleliebiao"></span>
          <span>
            <span
              @click="leftPlay"
              id="zuo"
              class="iconfont icon-shangyishou"
            ></span>

            <div @click="btn" class="stop">
              <i
                id="stop"
                class="iconfont icon-bofang2"
                v-show="singFlag === 1 ? true : false"
              ></i>
              <i
                id="stop"
                class="iconfont icon-bofang3"
                v-show="singFlag === 0 ? true : false"
              ></i>
            </div>
            <span
              id="you"
              @click="rightPlay"
              class="iconfont icon-xiayishou2"
            ></span>
          </span>
          <span @click="alerte" class="iconfont icon-fenxiang1"></span>
        </div>
      </div>
    </div>
    <transition name="v">
      <div class="zhe hua" v-if="alertFlag">
        <Alert>
          <div class="containerFun">
            <div class="containerFun_title">
              <div class="containerFun_text">
                <span><i>你可以享受一下歌曲</i></span>
              </div>
              <div @click="close" class="containerFun_close">
                <span class="iconfont icon-arrowdown"></span>
              </div>
            </div>
            <div class="containerFun_singList">
              <div
                @click="singClick(index)"
                class="sing_row"
                v-for="(item, index) in singList"
                :key="index"
              >
                <div class="sing_id_num">{{ index + 1 }}</div>
                <div class="sing_id_msg">
                  <p class="sing_id_title">{{ item.introduce }}</p>
                  <p class="sing_id_p">{{ item.author }}</p>
                </div>
              </div>
            </div>
          </div>
        </Alert>
      </div>
    </transition>
  </div>
</template>
<script>
import Alert from "./alert/index.vue";
import * as api from "../../../api/home";
export default {
  components: {
    Alert,
  },
  name: "",
  data() {
    return {
      msg: "测试",
      item: {},
      id: "",
      alertFlag: false,
      data: [],
      nowIndex: 3,
      // 要
      singList: [],
      singText: [],
      singFlag: 0,
      i: 0,
      pIndex: "",
      timer: null,
      // 评论
      bookId: "",
      talkItem: {},
      introduceId: "",
      talkSort: null,

      timerArr: [],
      timerFlag: true,
    };
  },
  created() {},
  mounted() {
    this.item = JSON.parse(this.$route.query.episode);
    this.introduceId = this.item.introduceId;
    // 歌词
    this.$store.dispatch("chat/getSingText").then((res) => {
      this.singText = res;
    });
    // 音乐
    this.$store.dispatch("chat/getMusic").then((res) => {
      this.singList = res;
      this.$nextTick(() => {
        // music
        if (this.musicFlag) {
            this.$refs.audio.play();
        }
      });
    });

    // new
    this.$nextTick(() => {
      let arr = JSON.parse(localStorage.getItem("timer"));
      if (arr !== null) {
        this.$refs.audio.currentTime = arr.nowTime;
        localStorage.removeItem("timer");
        console.log("222");
      }
    });

    api
      .toPingLun({ introduceId: this.introduceId })
      .then((res) => {
        res.data.sort((a, b) => {
          return a.talkId - b.talkId;
        });
        this.talkItem = res.data[Number(res.data.length) - 1];
        if (this.talkItem) {
          if (this.talkItem.talkCon.indexOf("mp3") === -1) {
            this.talkSort = 0;
          } else {
            this.talkSort = 1;
          }
        } else {
          this.talkSort = 2;
        }
      })
      .catch((err) => {
        console.log("失败", err);
      });
  },
  computed: {
    // 歌词
    list() {
      try {
        return this.singText[this.nowIndex].list;
      } catch (err) {
        return null;
      }
    },
    musicFlag() {
      this.singFlag = this.$store.state.chat.isplay ? 1 : 0;
      // 播放按钮
      return this.$store.state.chat.isplay || false;
    },
    singPath() {
      // 路径
      try {
        return this.singList[this.nowIndex].singPath;
      } catch (err) {
        return null;
      }
    },
  },
  methods: {
    alerte() {
      this.$router.push("/inviteFriend");
      this.$router.push({
        path: `/inviteFriend`,
        query:{ episode: JSON.stringify(this.item) }
      });
    },
    // 上传的时候丢一下代码；歌曲去00：
    // fastSings(e) {拖拽工能有瑕疵
    //     let x = Math.ceil(e.targetTouches[0].clientX) - this.$refs.dian.offsetLeft;
    //     this.fastSing(x);
    // },
    fastSing(offsetX) {
      // 点击进度条
      let allTime = this.$refs.audio.duration;
      let allowWidth = this.$refs.long.offsetWidth;
      let nowTime = (allTime / allowWidth) * offsetX;
      let moveWidth = (allowWidth * nowTime) / allTime;
      this.$refs.small.style.width = moveWidth + "px";
      this.$refs.dian.style.left = moveWidth + 49 + "px";
      this.$refs.audio.currentTime = nowTime;
      this.clearHighlight(2);
      this.singText[this.nowIndex].list.forEach((item, index) => {
        let i = index === 0 ? 0 : index - 1;
        let preTimes = this.fun(this.singText[this.nowIndex].list[i].time);
        let nowTimes = this.fun(item.time);
        if (preTimes <= nowTime && nowTime <= nowTimes) {
          this.clearHighlight(3, index);
          return;
        }
      });
    },

    fun(time) {
      // 计算数据库中歌词的时间
      let objs = time.split(":");
      let strf = Number(objs[0]) * 60;
      let strs = objs[1].split(".");
      let strss = Number(strs[0]);
      let nowTimes = strf + strss + Number("0." + strs[1]);
      return nowTimes;
    },
    tiao(allowWidth, nowTime, allTime) {
      // 条动态
      let moveWidth = (allowWidth * nowTime) / allTime;
      this.$refs.small.style.width = moveWidth + "px";
      this.$refs.dian.style.left = moveWidth + 49 + "px";
    },
    clearHighlight(type, index) {
      if (type === 1) {
        [...this.$refs.zyCon.children].forEach((item, index) => {
          item.style.color = "";
        });
        [...this.$refs.zyCon.children][index].style.color = "red";
        let n = [...this.$refs.zyCon.children][index];
        this.$refs.zyCon.scrollTo({
          top: n.offsetTop - 200,
          behavior: "smooth",
        });
      } else if (type === 2) {
        [...this.$refs.zyCon.children].forEach((item, index) => {
          item.style.color = "";
        });
      } else if (type === 3) {
        [...this.$refs.zyCon.children][index].style.color = "red";
        let n = [...this.$refs.zyCon.children][index];
        this.$refs.zyCon.scrollTo({
          top: n.offsetTop - 200,
          behavior: "smooth",
        });
      }
    },
    clickP(time, index) {
      // 点击歌词
      let allTime = this.$refs.audio.duration;
      let allowWidth = this.$refs.long.offsetWidth;
      let objs = time.split(":");
      let strf = Number(objs[0]) * 60;
      let strs = objs[1].split(".");
      let strss = Number(strs[0]);
      let nowTime = strf + strss + Number("0." + strs[1]);
      this.tiao(allowWidth, nowTime, allTime);
      this.$refs.audio.currentTime = nowTime;
      this.clearHighlight(1, index);
    },
    close() {
      this.alertFlag = false;
      this.setTime();
    },
    tan() {
      this.alertFlag = true;
    },
    singClick(index) {
      this.nowIndex = index;

      this.clearHighlight(2);
      setTimeout(() => {
        this.changeS();
      }, 150);
    },
    palyS() {
      console.log("111");
      this.setTime();
    },
    setTime() {
      console.log("222");
      this.timer = setInterval(() => {
        let allTime = this.$refs.audio.duration;
        let nowTime = this.$refs.audio.currentTime;

        let allowWidth = this.$refs.long.offsetWidth;
        let minus = Math.floor(allTime / 60);
        let second = Math.ceil(allTime % 60);
        let str = ``;
        if (minus < 10) {
          if (second < 10) {
            str = "0" + minus + ":" + "0" + second;
          } else {
            str = "0" + minus + ":" + second;
          }
        } else {
          if (second < 10) {
            str = minus + ":" + "0" + second;
          } else {
            str = minus + ":" + second;
          }
        }
        this.$refs.molTime.innerHTML = str;
        this.tiao(allowWidth, nowTime, allTime);
        let strm = Math.floor(nowTime / 60);
        let strs = Math.ceil(nowTime % 60);
        let ostr = "";
        if (strm < 10) {
          if (strs < 10) {
            ostr = "0" + strm + ":" + "0" + strs;
          } else {
            ostr = "0" + strm + ":" + strs;
          }
        } else {
          if (str < 10) {
            ostr = strm + ":" + "0" + strs;
          } else {
            ostr = strm + ":" + strs;
          }
        }
        this.$refs.nowTime.innerHTML = ostr;
        this.singText[this.nowIndex].list.forEach((item, index) => {
          if (item.time.indexOf(ostr) >= 0) {
            this.clearHighlight(1, index);
            return;
          }
        });

        // new

        this.timerArr.push({
          singId: "1001",
          nowTime,
        });
        if (this.timerFlag) {
          localStorage.setItem(
            "timer",
            JSON.stringify(this.timerArr[this.timerArr.length - 1])
          );
        }
      }, 1000);
    },
    rightPlay() {
      this.$refs.zyCon.scrollTop = 0;
      this.nowIndex++;
      if (this.nowIndex > this.singList.length - 1) {
        this.nowIndex = 0;
      }
      this.clearHighlight(2);
      setTimeout(() => {
        this.changeS();
      }, 150);
    },
    leftPlay() {
      this.$refs.zyCon.scrollTop = 0;
      this.nowIndex--;
      if (this.nowIndex < 0) {
        this.nowIndex = this.singList.length;
      }
      this.clearHighlight(2);
      setTimeout(() => {
        this.changeS();
      }, 150);
    },
    changeS() {
      switch (this.singFlag) {
        case 1:
          if (this.$refs.audio.paused) {
            this.$refs.audio.play();
          }
          break;
        case 0:
          if (!this.$refs.audio.paused) {
            this.$refs.audio.pause();
            clearInterval(this.timer);
          }
          break;
      }
    },
    btn() {
      this.singFlag === 0 ? (this.singFlag = 1) : (this.singFlag = 0);
      switch (this.singFlag) {
        case 1:
          if (this.$refs.audio.paused) {
            this.$refs.audio.play();
          }
          break;
        case 0:
          if (!this.$refs.audio.paused) {
            this.$refs.audio.pause();
            clearInterval(this.timer);
          }
          break;
      }
    },

    gohu() {
      this.$router.push({
        path: `/story/${this.item.bookId}`,
      });
      clearInterval(this.timer);
    },
    goTalk() {
      this.$router.push({
        path: `/pinglun/${this.item.introduceId}`,
      });
    },
  },

  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<style scoped>
@import url("../../../../assets/font/iconfont.css");
@import url("../../../../assets/font_01aelaoiozjn/iconfont.css");
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.box {
  background: rgb(0, 179, 151);
  width: 100%;
  color: white;
  height: 100vh;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
}
.animate__animated.animate__bounceInUp {
  --animate-duration: 1s;
}

:root {
  --animate-duration: 800ms;
  --animate-delay: 0.9s;
}
.tit-1 {
  width: 100%;
  height: 44px;
  /* background: wheat; */
  display: flex;
  justify-content: space-between;
  text-align: center;
  line-height: 44px;
}
.tit-1-left {
  margin-left: 16px;
  font-weight: 600;
}
.tit-1-right {
  margin-right: 16px;
}
.tit-1-right i {
  line-height: 44px;
  font-size: 32px;
}
.con-low p {
  margin: 3px 0;
}
.con {
  margin: 0 auto;
  width: 88%;
  text-align: center;
  height: calc(100vh - 66px);
}
.con-tit {
  font-size: 16px;
  margin: 20px 0;
}
.con-center {
  margin: 20px 0;
  font-size: 14px;
}
/* 播放器 */
.process {
  width: 350px;
  height: 50px;
  position: absolute;
  bottom: 100px;
  margin: 0px -175px;
  left: 50%;
  font-size: 12px;
  font-family: Arial, Helvetica, sans-serif;
}

.process .process-bar {
  position: absolute;
  left: 36px;
  width: 280px;
  margin-top: 5px;
  background-color: #615d5c;
}

.process-bar .rdy {
  background-color: #b1adac;
  height: 2px;
}

.process-bar .cur {
  background-color: #fb0d0d;
  height: 2px;
  position: absolute;
  top: 0;
  left: 0;
}

.cur .process-btn {
  cursor: pointer;
  background-size: cover;
  position: absolute;
  top: -9px;
  right: -13px;
  width: 22px;
  height: 22px;
  margin-left: -11px;
}
.comment-1 {
  width: 88%;
  text-align: left;
  height: 30px;
  margin-left: 21px;
  position: absolute;
  bottom: 150px;
}
.comment {
  width: 88%;
  height: 30px;
  background: white;
  line-height: 30px;
  border-radius: 15px;
  color: black;
  font-size: 12px;
  float: left;
  overflow: hidden;
}

.comment span {
  color: #979797;
  float: left;
  margin: 0 10px;
}
.comment img {
  float: left;
  width: 25px;
  margin: 2px 0 0 5px;
}
#msg i {
  font-size: 15px;
  margin-left: 7px;
}
#msg {
  width: 30px;
  height: 30px;
  font-size: 14px;
  color: rgb(0, 179, 151);
  border-radius: 30px;
  background: white;
  float: right;
  line-height: 30px;
}

/* 音乐播放 */
.music {
  width: 90%;
  margin: 35px auto 0;
  height: 110px;
  margin-left: 20px;
  margin-top: 35px;
  /* background: pink; */
  float: left;
  position: absolute;
  bottom: 0;
}
.plan {
  width: 100%;
  height: 12px;
  font-size: 10px;
}
.plan span {
  display: inline-block;
}
.small {
  width: 4px;
  height: 2px;
  /* line-height: 100%; */
  background: white;
  position: absolute;
  top: 8px;
  z-index: 99;
}

.long {
  width: 70%;
  height: 1px;
  position: relative;
  top: -3px;
  left: 0;
  background: rgba(40, 189, 166, 0.5);
}
.dian {
  width: 8px;
  height: 8px;
  border-radius: 8px;
  background: white;
  position: absolute;
  top: 5px;
  left: 53px;
  z-index: 99;
}
.time {
  padding: 0;
}

.music-down {
  width: 79%;
  height: 80px;
  margin: 15px auto 0;
  /* background: blueviolet; */
  display: flex;
  justify-content: space-between;
  text-align: center;
  font-size: 26px;
}
.music-down > span {
  font-size: 26px;
  line-height: 50px;
}
#stop {
  font-size: 26px;
  line-height: 50px;
}
.stop {
  width: 50px;
  height: 50px;
  margin-top: 0;
  border: 1px solid white;
  border-radius: 50px;
  display: inline-block;
}
#zuo {
  /* margin-top:0 ; */
  position: absolute;
  top: 28px;
  left: 115px;
}
#you {
  display: inline-block;
  font-size: 22px;
  position: absolute;
  top: 27px;
  left: 195px;
}
.zyCon {
  width: 70%;
  height: 350px;
  overflow-y: auto;
  margin: 10px auto;
}
.inner-container::-webkit-scrollbar {
  display: none;
}
.zyCon p {
  font-size: 15px;
}
/* 777 */
.containerFun {
  width: 414px;
  height: 200px;
  background-color: white;
  border-radius: 16px 16px 0 0;
  position: fixed;
  bottom: -1px;
  left: 0;
}
.containerFun_title {
  width: 100%;
  height: 50px;
  display: flex;
  color: #d5d5d5;
}
.containerFun_text {
  flex: 0.9;
  line-height: 50px;
  text-align: center;
  font-size: 13px;
}
.containerFun_close {
  flex: 0.1;
  line-height: 50px;
  text-align: center;
}
.containerFun_singList {
  width: 417px;
  height: calc(100% - 50px);
  overflow: auto;
}
.sing_row {
  width: 417px;
  height: 70px;
  display: flex;
  color: #000;
  display: flex;
}
.sing_id_num {
  flex: 0.1;
  text-align: center;
  line-height: 70px;
  font-size: 15px;
}
.sing_id_msg {
  flex: 0.9;
  font-size: 13px;
  padding: 15px;
}
.sing_id_msg p:nth-child(1) {
  font-size: 15px;
}
.sing_id_msg p:nth-child(2) {
  color: #615d5c;
}
.zhe {
  width: 417px;
  height: 200px;
  border-radius: 16px 16px 0 0;
  position: fixed;
  left: 0;
  bottom: 0;
}
.hua {
  transition: all 0.3s linear;
}
.v-enter {
  transform: translateY(100%);
}
.v-leave-to {
  transform: translateY(100%);
}
</style>